import time

import allure
from selenium import webdriver

from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from weixin_po.utils.log_util import logger

"父类"


class BasePage:
    _Base_url = 'https://work.weixin.qq.com/wework_admin/frame#index'
    _Remote_url = 'http://127.0.0.1:31080'

    def __init__(self, driver=None):

        self.options = webdriver.ChromeOptions()
        self.options.add_argument("--disable-infobars")
        self.options.add_argument("--disable-dev-shm-usage")
        self.options.add_argument("--no-sandbox")
        # option.add_argument("--headless")
        self.options.add_argument("--lang=zh-CN")
        self.options.add_argument("--disable-extensions")
        self.options.add_argument("--ignore-ssl-errors")
        self.options.add_argument("--ignore-certificate-errors")
        self.options.add_argument('--disable-gpu')
        self.prefs = {'download.default_directory': '/home/seluser/Downloads/'}
        self.caps = {"browserName": "chrome", "browserVersion": "122.0"}
        self.options.add_experimental_option('prefs', self.prefs)
        self.options.add_experimental_option('w3c', self.caps)

        if driver:

            self.driver = driver
        else:
            self.driver = webdriver.Remote(command_executor=self._Remote_url,
                                           keep_alive=True,
                                           options=self.options
                                           )
            self.driver.implicitly_wait(5)
            self.driver.maximize_window()
        if not self.driver.current_url.startswith("http"):
            self.driver.get(self._Base_url)

    def get_url(self):

        """打开页面"""
        return self.driver.get(self._Base_url)

    def find(self, by, locator=None):
        """获取单个元素"""
        logger.info(f"查找元素：by>{by},元素定位>{locator}")
        if locator:
            return self.driver.find_element(by, locator)
        else:
            return self.driver.find_element(*by)

    def finds(self, by, locator=None):
        if locator:
            return self.driver.find_elements(by, locator)
        else:
            return self.driver.find_elements(*by)

    def find_and_click(self, by, locator: None):
        """查找后点击"""
        self.find(by, locator).click()

    def find_send_keys(self, value, by, locator=None):
        element = self.find(by, locator)
        element.clear()
        element.send_keys(value)

    def quit(self):
        logger.info("退出 qiut()")

    def get_screen(self):
        timesnow = int(time.time())
        # 一定要提前创建好images路径
        image_path = f"./image/image_{timesnow}.png"
        self.driver.save_screenshot(image_path)
        allure.attach.file(image_path, name="picture", attachment_type=allure.attachment_type.PNG)

    def wait_for_ele_to_be_visible(self, by, locator):
        return WebDriverWait(self.driver, 10, 2).until(
            expected_conditions.visibility_of_element_located((by, locator))
        )

    def wait_for_ele_to_be_click(self, by, locator):
        WebDriverWait(self.driver, 10, 2).until(
            expected_conditions.element_to_be_clickable((by, locator))
        ).click()
